﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Joker.Model;
using Joker.View.Editor;
using Model;

namespace Joker.View.SubForm {
    public partial class WarehouseManage : Form {
        string sqlstr = $"select * from Warehouse";
        public WarehouseManage() {
            InitializeComponent();
        }

        private void WarehouseManage_Load(object sender, EventArgs e) {
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlstr);
            cmbSex.SelectedIndex = 0;
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
            if (e.RowIndex > 0 && e.RowIndex < dataGridView1.Rows.Count) dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[0];
        }

        private void button1_Click(object sender, EventArgs e) {
            string name = this.txtName.Text;
            string lxr = this.txtlianxiren.Text;
            string sex = this.cmbSex.Text;
            string tel = this.txtPhone.Text;
            string sql = $"{sqlstr} where Name like '%{name}%' and Contact like '%{txtlianxiren.Text}%' and Gender = {(cmbSex.SelectedItem.ToString().Equals("男") ? "1" : "0")} and Tel like '%{txtPhone.Text}%'";
            dataGridView1.DataSource = DBHelper.GetDataTable(sql);
        }

        private void 新增ToolStripMenuItem_Click(object sender, EventArgs e) {
            WarehouseDataEdit form = new WarehouseDataEdit();
            OpenForm.NewForm(this,form,false);
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlstr);
        }

        private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) {
            WarehouseDataEdit form = new WarehouseDataEdit();
            form.WarehouseID = (int)dataGridView1.CurrentRow.Cells[0].Value;
            OpenForm.NewForm(this, form, false);
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlstr);
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {
            if (MessageBox.Show($"确定删除ID为{dataGridView1.CurrentRow.Cells[0].Value}的数据?","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.Yes) {
                if (DBHelper.ExecuteNonQuery($"delete from Warehouse where ID = {dataGridView1.CurrentRow.Cells[0].Value}")) {
                    MessageBox.Show("删除成功!");
                } else {
                    MessageBox.Show("删除失败!");
                }
            }
            dataGridView1.DataSource = DBHelper.GetDataTable(sqlstr);
        }
    }
}
